import { Effect } from 'dva';
import { Reducer } from 'redux';

import { cptAdd, cptEdit, cptList, cptDelete, cptUpload } from '@/services/pay';
import { notification } from 'antd';

export interface CurrentUser {
  avatar?: string;
  name?: string;
  title?: string;
  group?: string;
  signature?: string;
  tags?: {
    key: string;
    label: string;
  }[];
  userid?: string;
  unreadCount?: number;
}

export interface State {
  total?: number;
  isRefresh?: boolean;
  list?: Array<any>;
  view?: {};
  totalprofit?: {};
}

export interface UserModelType {
  namespace: 'pay';
  state: State;
  effects: {
    add: Effect;
    edit: Effect;
    delete: Effect;
    list: Effect;
    view: Effect;
    upload: Effect;
  };
  reducers: {
    redList: Reducer<State>;
    reFresh: Reducer<State>;
    redView: Reducer<State>;
  };
}

const UserModel: UserModelType = {
  namespace: 'pay',

  state: {
    total: 0,
    isRefresh: false,
    list: [],
    view: {},
  },

  effects: {
    *add({ payload }, { call, put }) {
      try {
        const response = yield call(cptAdd, payload);
        if (response.status == 200) {
          notification.success({
            message: '新增成功',
            description: '',
          });
          yield put({
            type: 'reFresh',
          });
        }
      } catch (error) {}
    },
    *edit({ payload }, { call, put }) {
      try {
        const response = yield call(cptEdit, payload);
        if (response.status == 200) {
          notification.success({
            message: '修改成功',
            description: '',
          });
          yield put({
            type: 'reFresh',
          });
        }
      } catch (error) {}
    },
    *delete({ payload }, { call, put }) {
      try {
        const response = yield call(cptDelete, { ids: payload.id });
        if (response.status == 200) {
          notification.success({
            message: '删除成功',
            description: '',
          });
          yield put({
            type: 'reFresh',
          });
        }
      } catch (error) {}
    },
    *list({ payload }, { call, put }) {
      try {
        const response = yield call(cptList, payload);
        if (response.status == 200) {
          yield put({
            type: 'redList',
            payload: response.data.content.content,
            total: response.data.content.totalElements,
          });
        }
      } catch (error) {
        console.log(error);
      }
    },
    *upload({ payload }, { call, put }) {
      try {
        const response = yield call(cptUpload, payload);
        if (response.status == 200) {
          notification.success({
            message: '上传成功',
            description: '',
          });
        }
      } catch (error) {
        console.log(error);
      }
    },
    *view({ payload }, { call, put }) {
      yield put({
        type: 'redView',
        payload: payload,
      });
    },
  },

  reducers: {
    redList(state, action) {
      return {
        ...state,
        list: action.payload,
        isRefresh: false,
        total: action.total,
        totalprofit: action.totalprofit,
      };
    },
    redView(state, action) {
      return {
        ...state,
        view: action.payload,
      };
    },
    reFresh(state, action) {
      return {
        ...state,
        isRefresh: true,
      };
    },
  },
};

export default UserModel;
